package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.Beans.ErroInfoBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.ViewModels.LoginViewModel;
import com.echoesnet.echoesshortvideo.ViewModels.StepRegisterViewModel;
import com.echoesnet.echoesshortvideo.views.dialog.CustomAlertDialog;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AlterPassWordAct extends BaseActivity
{
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.okPassword)
    EditText okPassword;
    @BindView(R.id.oldPassword)
    EditText oldPassword;
    @BindView(R.id.nextOk)
    TextView nextOk;
    private LoginViewModel lModel;
    private Activity mAct;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_pass_word);
        ButterKnife.bind(this);
        mAct=this;

        lModel = new LoginViewModel();
        lModel.getLoginLive().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String type)
            {
                if (type == null)
                    return;
                ToastUtils.showShort("修改密码成功");
                finish();
            }
        });

        lModel.getLoginErroerLive().observe(this, new Observer<ErroInfoBean>()
        {
            @Override
            public void onChanged(@Nullable ErroInfoBean erroInfoBean)
            {
                new CustomAlertDialog(mAct)
                        .builder()
                        .setMsg("您输入的登录密码错误\n请重新输入")
                        .setPositiveButton("确认", v ->
                        {
                        })
                        .setMsgColor(ContextCompat.getColor(mAct,R.color.black))
                        .show();
            }
        });

        password.addTextChangedListener(textWatchernew);
        okPassword.addTextChangedListener(textWatchernew);
        oldPassword.addTextChangedListener(textWatchernew);

        nextOk.setAlpha(0.5f);
        nextOk.setClickable(false);

    }

    @OnClick({R.id.cancle, R.id.nextOk})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.cancle:
                finish();
                break;
            case R.id.nextOk:
                if(!CommonUtils.verifyInput(1, password.getText().toString().trim()))
                {
                    new CustomAlertDialog(mAct)
                            .builder()
                            .setMsg("请设置密码为8-16位\n字母数字组合的密码")
                            .setPositiveButton("确认", v ->

                            {
                            })
                            .setMsgColor(ContextCompat.getColor(mAct,R.color.black))
                            .show();
                    return;
                }

               String sOkPassword= okPassword.getText().toString().trim();
               String sPassword= password.getText().toString().trim();

                if(!sOkPassword.equals(sPassword))
                {
                    new CustomAlertDialog(mAct)
                            .builder()
                            .setMsg("您输入的新密码不一致\n请重新输入")
                            .setPositiveButton("确认", v ->
                            {
                            })
                            .setMsgColor(ContextCompat.getColor(mAct,R.color.black))
                            .show();
                    return;
                }

                lModel.alertPassWord(oldPassword.getText().toString().trim(),
                        password.getText().toString().trim());
                break;
        }
    }

    TextWatcher textWatchernew =new TextWatcher()
{
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {

    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        changeBtn();
    }
    @Override
    public void afterTextChanged(Editable s)
    {

    }
};


    private  void changeBtn()
    {
        if(TextUtils.isEmpty(password.getText().toString().trim())||TextUtils.isEmpty(okPassword.getText().toString().trim())||TextUtils.isEmpty(oldPassword.getText().toString().trim()))
        {
            nextOk.setClickable(false);
            nextOk.setAlpha(0.5f);
        }
        else
        {
            nextOk.setClickable(true);
            nextOk.setAlpha(1f);
        }
    }



}
